package com.sihan.framework.etm.test;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

public class AdminPasswordGenerator {

    public static void main(String[] args) {
        BigDecimal bigDecimal = FormattedQuantity(new BigDecimal("3.5"));
        System.out.println(bigDecimal);
    }

    public static BigDecimal FormattedQuantity(BigDecimal voltage) {
        //建立百分比格式化引用
        NumberFormat percent = NumberFormat.getPercentInstance();
        BigDecimal maxVoltage = new BigDecimal("4.60");
        BigDecimal minVoltage = new BigDecimal("3.30");
        if (null != voltage && voltage.compareTo(BigDecimal.ZERO) != 0) {
            //如果电压大于或等于4.6 设备电量100%
            if (voltage.compareTo(maxVoltage) > -1) {
                return new BigDecimal("100.00");
            }
            //如果电压小于于或等于3.3 设备电量0%
            if (minVoltage.compareTo(voltage) > -1) {
                return BigDecimal.ZERO;
            }
            //如果电压在4.6-.3.区间 则取百分百
            BigDecimal subtract = voltage.subtract(minVoltage);
            BigDecimal divide = (subtract.divide(new BigDecimal("13.0"), 1, RoundingMode.UP).setScale(2, RoundingMode.UP)).multiply(new BigDecimal("100.0"));
            return divide;
        }
        return BigDecimal.ZERO;
    }

}
